package com.kupurui.asteacher.ui.mine.person;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kupurui.asteacher.R;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.MyCount;
import com.kupurui.asteacher.utils.UserManger;
import com.mob.MobSDK;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import info.hoang8f.widget.FButton;

/**
 * 更换手机号码
 * Created by Administrator on 2017/7/18.
 */

public class PhoneChangeAty extends BaseAty {

    @Bind(R.id.tv_bar_title)
    TextView tvBarTitle;
    @Bind(R.id.m_toolbar)
    Toolbar mToolbar;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.edit_get_verify)
    EditText editGetVerify;
    @Bind(R.id.fbtn_get_verify)
    FButton fbtnGetVerify;
    @Bind(R.id.fbtn_change_phone)
    FButton fbtnChangePhone;

    MyCount myCount;
    EventHandler eh;

    private String phone = "";
    private String code = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MobSDK.init(this,"20d37998dc243","27735b9dab2c4328b3e6d9f3bff6ab3a");
        eh = new EventHandler(){
            public void afterEvent(int arg0, int arg1, Object arg2) {  //afterEvent 为调用SDK之后回调的方法
                Message m = Message.obtain();
                m.what = 1;
                m.arg1 = arg0;//event
                m.arg2 = arg1;//result
                handler.sendMessage(m);
            }
        };

        SMSSDK.registerEventHandler(eh);//注册短信回调接口
    }

    Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if(msg.arg2 == SMSSDK.RESULT_COMPLETE){//发送成功的情况
                        if(msg.arg1 == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE){//验证成功通过
                            startActivity(PhoneBindingAty.class,null);
                            Log.e("短信验证","验证通过");
                        }else if(msg.arg1 == SMSSDK.EVENT_GET_VERIFICATION_CODE){//验证码已经从服务器发出
                            dismissLoadingDialog();
                            myCount.start();
                            showToast("验证码已发出,请注意查收");
                        }
                    }else{
                        showToast("验证码错误");
                        dismissLoadingDialog();
                    }
                    break;
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.phone_change_aty;
    }

    @Override
    public void initData() {
        initToolbar(mToolbar,"更换手机号码");
        myCount = new MyCount(60000, 1000, fbtnGetVerify);

        phone = UserManger.getUserInfo().getPhone();
        StringBuilder sb  =new StringBuilder();
        for (int i = 0; i < phone.length(); i++) {
            char c = phone.charAt(i);
            if (i >= 3 && i <= 6) {
                sb.append('*');
            } else {
                sb.append(c);
            }
        }
        tvPhone.setText(sb.toString());
    }

    @Override
    public void requestData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myCount.cancel();
        SMSSDK.unregisterEventHandler(eh);
    }

    @OnClick({R.id.fbtn_change_phone,R.id.fbtn_get_verify})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.fbtn_change_phone:
                code = editGetVerify.getText().toString();
                if (TextUtils.isEmpty(code)){
                    showToast("请输入验证码");
                    return;
                }
                SMSSDK.submitVerificationCode("86", phone, code);//提交短信验证码，在监听中返回，str :手机号 str1:收到的验证码
                break;
            case R.id.fbtn_get_verify:
                showLoadingDialog("");
                SMSSDK.getVerificationCode("86",phone);//发送验证码
                break;
        }
    }
}
